#include<stdio.h>
#include<stdlib.h>
#include "tree.h"
#include "linkqueue.h"

//创建二叉树
bitree * tree_create(){
	//采用先序遍历方式创建二叉树
	data_t value;
	scanf("%c", &value);
	if(value == '#'){
		return NULL;
	}
	bitree * head = (bitree *)malloc(sizeof(bitree));
	head->data = value;
	head->left = tree_create();
	head->right = tree_create();
	return head;
}
//二叉树的前序遍历
void preorder(bitree *r){
	if(r == NULL){
		return;
	}
	printf("%c",r->data);
	preorder(r->left);
	preorder(r->right);
}
//二叉树的中序遍历
void ineorder(bitree *r){
	if(r == NULL){
		return;
	}
	ineorder(r->left);
	printf("%c",r->data);
	ineorder(r->right);
}
//二叉树的后序遍历
void posteorder(bitree *r){	
	if(r == NULL){
		return;
	}
	posteorder(r->left);
	posteorder(r->right);
	printf("%c",r->data);
}
//二叉树的层序遍历
void layerorder(bitree *r){
	if(r == NULL){
		return;
	}
	//创建队列
	linkqueue *  lq = queue_create();
	printf("%c",r->data);
	enqueue(lq, r);
	while(!queue_empty(lq)){
		r = dequeue(lq);
		if(r->left != NULL){
			printf("%c",r->left->data);
			enqueue(lq, r->left);		
		}
		if(r->right != NULL){
			printf("%c",r->right->data);
			enqueue(lq, r->right);
		}
	}
}
